package com.oceansoft.mobile.biz.api.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.coobird.thumbnailator.Thumbnails;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.oceansoft.mobile.biz.api.entity.Dog;
import com.oceansoft.mobile.biz.api.entity.Owner;
import com.oceansoft.mobile.biz.api.service.IApiOwnerDogService;
import com.oceansoft.mobile.common.util.Base64Utils;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.common.util.UuidUtils;
import com.oceansoft.mobile.common.util.ValidateCardUtils;
import com.oceansoft.mobile.weixin.util.WeixinUtil;

@Controller
@RequestMapping("api/owner_dog")
public class ApiOwnerDogController {

	public static final String GR = "1";
	public static final String DW = "2";
	public static final String WJ = "3";

	@Resource
	private IApiOwnerDogService apiOwnerDogService;

	@RequestMapping("exec")
	public String doExecute(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/exec";
	}

	@RequestMapping("zxbl")
	public String zxbl(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/zxbl";
	}

	@RequestMapping("progress")
	public String getProgressInfo(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/yqbjjdcx";
	}
/**
 * 养犬办件进度查询
 * @param request
 * @param zjhm
 * @param model
 * @return
 */
	@RequestMapping("yqjdcx")
	public String getYqjdcx(HttpServletRequest request,String zjhm, ModelMap model) {
		//WeixinUtil.gesearchCountInfo("15","其他");//统计方法调用（暂时用不到）
		List<Dog> dogs = apiOwnerDogService.getDogProcessList(zjhm);
		model.put("dogs", dogs);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/yqbjjdlist";
	}

	@RequestMapping("yqbjdetail")
	public String getYqbjDetail(String dogId, HttpServletRequest request,ModelMap model) {
		Dog dog = apiOwnerDogService.getDogProcessDetail(dogId);
		if ("1".equals(dog.getSexId())) {
			dog.setSexId("雌");
		} else {
			dog.setSexId("雄");
		}
		dog.setTypeId(apiOwnerDogService.getQz(dog.getTypeId()));
		if ("1".equals(dog.getYtId())) {
			dog.setYtId("玩赏犬");
		} else if ("2".equals(dog.getYtId())) {
			dog.setYtId("导盲犬");
		} else if ("3".equals(dog.getYtId())) {
			dog.setYtId("护助犬");
		} else if ("4".equals(dog.getYtId())) {
			dog.setYtId("看护犬");
		} else if ("5".equals(dog.getYtId())) {
			dog.setYtId("其他");
		}
		if ("1".equals(dog.getLyId())) {
			dog.setLyId("购买");
		} else if ("2".equals(dog.getLyId())) {
			dog.setLyId("赠送");
		} else if ("3".equals(dog.getLyId())) {
			dog.setLyId("转让");
		} else if ("4".equals(dog.getLyId())) {
			dog.setLyId("收养");
		} else if ("5".equals(dog.getLyId())) {
			dog.setLyId("繁殖");
		} else if ("6".equals(dog.getLyId())) {
			dog.setLyId("其他");
		}
		if ("1".equals(dog.getAqcsId())) {
			dog.setAqcsId("笼养");
		} else if ("2".equals(dog.getAqcsId())) {
			dog.setAqcsId("圈养");
		} else if ("3".equals(dog.getAqcsId())) {
			dog.setAqcsId("拴养");
		} else if ("5".equals(dog.getAqcsId())) {
			dog.setAqcsId("嘴罩");
		} else if ("5".equals(dog.getAqcsId())) {
			dog.setAqcsId("绝育");
		} else if ("6".equals(dog.getAqcsId())) {
			dog.setAqcsId("其他");
		}
		 
		
		if("1".equals(dog.getLyfsId())){
			dog.setLyfsId("个人");
		}
		if("2".equals(dog.getLyfsId())){
			dog.setLyfsId("单位");
		}
		if("3".equals(dog.getLyfsId())){
			dog.setLyfsId("外籍");
		}
		
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		model.put("dog", dog);
		return "weixin/ownerdog/yqbjjd";
	}

	@RequestMapping("js")
	public String js(HttpServletRequest request,Owner owner, ModelMap model) {
		model.put("zjhm", owner.getZjhm());
		model.put("sex", ValidateCardUtils.validateSex(owner.getZjhm()));
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		String resultPage = null;
		if (GR.equals(owner.getQzlx())) {
			resultPage = "weixin/ownerdog/grqzdj";
		}
		if (DW.equals(owner.getQzlx())) {
			resultPage = "weixin/ownerdog/dwqzdj";
		}
		if (WJ.equals(owner.getQzlx())) {
			resultPage = "weixin/ownerdog/wjqzdj";
		}
		return resultPage;
	}

	@RequestMapping("getdept")
	@ResponseBody
	public Result getDept(String key, String deptId) {
		Result re = new Result(Result.SUCC);
		String result = apiOwnerDogService.getDept(key, deptId);
		if(!"0".equals(result)){
			JSONArray jsons = JSON.parseArray(result);
			re.setData(jsons);
		}
		return re;
	}

	@RequestMapping("getloglx")
	@ResponseBody
	public Result getDogLx() {
		String result = apiOwnerDogService.getDogLx();
		JSONArray jsons = JSON.parseArray(result);
		Result re = new Result(Result.SUCC);
		re.setData(jsons);
		return re;
	}

	@RequestMapping("getcountry")
	@ResponseBody
	public Result getCountry() {
		String result = apiOwnerDogService.getCountry();
		JSONArray jsons = JSON.parseArray(result);
		Result re = new Result(Result.SUCC);
		re.setData(jsons);
		return re;
	}

	@RequestMapping(value = "sowner")
	public String saveOwner(Owner owner, HttpServletRequest request,
			ModelMap model) {
		apiOwnerDogService.saveOwner(owner, request);
		model.put("ownerId", owner.getUid());
		model.put("qzlx", owner.getQzlx());
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/dog";
	}

	@RequestMapping(value = "sdog")
	public String saveDog(Dog dog, HttpServletRequest request, ModelMap model) {
		apiOwnerDogService.saveDog(dog, request);
		model.put("ownerId", dog.getOwnerId());
		model.put("dogId", dog.getUid());
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/tjym";
	}

	@RequestMapping("submit")
	public String doSubmit(String ownerId,HttpServletRequest request, String dogId, ModelMap model) {
		String result = apiOwnerDogService.doSubmit(ownerId, dogId,request);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		if ("0".equals(result)) {
			return "weixin/ownerdog/error";
		}
		return "weixin/ownerdog/success";
	}

	@RequestMapping("error")
	public String success(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/error";
	}

	@RequestMapping("tj")
	public String tj(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/tjym";
	}

	@RequestMapping("jjyw")
	public String getJjywInfo(HttpServletRequest request, Model model) {
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/jjyw";
	}

	@RequestMapping(value = "{dq}", method = RequestMethod.GET)
	public String gotoPages(@PathVariable("dq") int dq,HttpServletRequest request, Model model) {
		model.addAttribute("dq", dq);
		System.out.println(dq);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/jjywxq";
	}

	@RequestMapping("dog")
	public String dog(HttpServletRequest request, Model model){
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/dog";
	}

	
	@RequestMapping("zxdt")
	@ResponseBody
	public Result getZxdtList(String pageIndex,String pageSize){
		Result re = new Result(Result.SUCC);
		String result= apiOwnerDogService.getZxdtList(pageIndex, pageSize);
		if(StringUtils.isNotBlank(result) && !"0".equals(result)){
			//JSONArray jsons = JSON.parseArray(result);
			//re.setData(jsons);
			JSON josnJson = JSON.parseObject(result);
			re.setData(josnJson);
		}
		return re;
	}
	
	@RequestMapping("zxdt_page")
	public String zxdtPage(HttpServletRequest request, Model model){
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/zxdt";
	}
	@RequestMapping(value = "zxdt_xq/{id}", method = RequestMethod.GET)
	public String zxdtPageXq(@PathVariable("id") String id,HttpServletRequest request,Model model){
		model.addAttribute("id", id);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/ownerdog/zxdtxq";
	}
	@RequestMapping(value = "zxdt_xqx/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Result getZxdt(@PathVariable("id") String id){
		
		Result re = new Result(Result.SUCC);
		String result= apiOwnerDogService.getZxdtInfo(id);
		if(StringUtils.isNotBlank(result) && !"0".equals(result)){
			//JSONArray jsons = JSON.parseArray(result);
			//re.setData(jsons);
			JSON josnJson = JSON.parseObject(result);
			re.setData(josnJson);
		}
		return re;
	}
	 /**
     *施工车辆图片上传2
     * @param base64
     * @return
     */
    @ResponseBody
    @RequestMapping(value="uploadBase64Img",method = RequestMethod.POST)
    public Result uploadMsg(HttpServletRequest request){
       /* //type字段用于识别前台传过来的不同图片
        String base64 = request.getParameter("base64");
        String seesionname = request.getParameter("imgname");
        //把base64对应的图片存放到session中
        HttpSession session = request.getSession();
        session.setAttribute(seesionname,base64);*/
    	MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
    	String type=request.getParameter("type");
		MultipartFile multipartFileqztxstr =  multipartHttpServletRequest.getFile("pzctrl"+type);
		try {
			InputStream qztxstrStream =  multipartFileqztxstr.getInputStream();
			long qztxstrLength = multipartFileqztxstr.getSize();
			System.out.println("qztxstrLength: "+ qztxstrLength);
			File parentFile = new File("/files/ownerdog");
			if(!parentFile.exists()){
				parentFile.mkdirs();
			}
			 HttpSession session = request.getSession();
			if(qztxstrLength>102400){
				File qztxstrFile = new File(parentFile,UuidUtils.getUpperUuid()+".jpeg");
				Thumbnails.of(qztxstrStream).scale(0.25f).toFile(qztxstrFile);   //按照百分之15压缩
				if(Integer.parseInt(type)>=4)
				{
					session.setAttribute("dogimg"+type,Base64Utils.getBase64(qztxstrFile));
				}else
				{
					session.setAttribute("ownerimg"+type,Base64Utils.getBase64(qztxstrFile));
					//String a=Base64Utils.getBase64(qztxstrFile);
				}
			     
				//owner.setQztxstr(Base64Utils.getBase64(qztxstrFile));
			}else{
				if(Integer.parseInt(type)>=4)
				{
					session.setAttribute("dogimg"+type,Base64Utils.getBase64(qztxstrStream));
				}
				else
				{
					session.setAttribute("ownerimg"+type,Base64Utils.getBase64(qztxstrStream));
					//String b=Base64Utils.getBase64(qztxstrStream);
				}
				
				//owner.setQztxstr(Base64Utils.getBase64(qztxstrStream));
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new Result("提交成功");
    }
    
    
//    
//    public static void toFile(String base64Code, String targetPath)
//    		   throws Exception {
//
//    		  byte[] buffer = base64Code.getBytes();
//    		  FileOutputStream out = new FileOutputStream(targetPath);
//    		  out.write(buffer);
//    		  out.close();
//    		 }
}
